package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.TaskInfoDao;
import cn.topevery.garden.entity.po.TaskInfo;
import cn.topevery.garden.entity.query.TaskInfoQuery;
import cn.topevery.garden.entity.vo.TaskInfoVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 任务信息
 *
 * @author code-generate
 * @date 2021-08-26
 */
@Repository
public class TaskInfoDaoImpl extends BaseDaoImpl<TaskInfo> implements TaskInfoDao {
    @Override
    public <P extends DbCsp> List<TaskInfo> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<TaskInfoVO> listPage(TaskInfoQuery query) {
        String table = "t_task_info tti\n" +
                "LEFT JOIN t_task_person_relation ttpr ON tti.c_id = ttpr.c_task_id \n" +
                "AND ttpr.c_db_status = 0\n" +
                "LEFT JOIN t_person tp ON ttpr.c_person_id = tp.c_id \n" +
                "AND tp.c_db_status = 0\n" +
                "LEFT JOIN t_car tc ON tc.c_task_id = tti.c_id \n" +
                "AND tc.c_db_status = 0 ";

        String columns = "tti.c_id id,\n" +
                "tti.c_name name,\n" +
                "tti.c_person_name personName,\n" +
                "tti.c_extent_id extentId,\n" +
                "tti.c_extent_name extentName,\n" +
                "IFNULL( tp.c_name, '' ) userName,\n" +
                "GROUP_CONCAT( tc.c_car_num ) carName";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tti.c_id desc");
        sqlQueryMate.where(true, "tti.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getExtentId() != null, "tti.c_extent_id", ScriptConditionEnum.Eq, query.getExtentId())
                .where(query.getTypeId() != null && query.getTypeId() > 0, "tti.c_type", ScriptConditionEnum.Eq, query.getTypeId())
                .where(StringUtils.isNotBlank(query.getName()), "tti.c_name", ScriptConditionEnum.Like, query.getName());
        sqlQueryMate.setGroupBy("tc.c_task_id,\n" +
                "tti.c_id,\n" +
                "tti.c_name,\n" +
                "tti.c_person_name,\n" +
                "tti.c_extent_id,\n" +
                "tp.c_name,\n" +
                "tti.c_extent_name ");
        return getDataPager(sqlQueryMate, pageSize, pageIndex, TaskInfoVO.class);
    }
}
